/*
 * Filename : Duration.java
 * Description : **COMPLETE**
 * History  : Date               Engineer       Description
 *            ---------          ------------   --------------------------------
 *            August 24, 2007      waitek         Initial creation.
 */

package com.lct.eq2.data;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Duration implements Serializable {
    static final long serialVersionUID = -7606256117598145404L;
    
    private Date startDate;
    private short hour;
    private short min;
    private short sec;
    private long totalSeconds = -1;
    
    /** Creates a new instance of Duration */
    public Duration() {
    }
    
/** Creates a new instance of Duration */
    public Duration(String duration) {
        if (duration.contains(":")) {
            if (duration.lastIndexOf(":") > duration.indexOf(":")) {
                String[] split = duration.split(":");
                this.setHour(Short.parseShort(split[0]));
                this.setMin(Short.parseShort(split[1]));
                this.setSec(Short.parseShort(split[2]));
            } else {
                String[] split = duration.split(":");
                this.setMin(Short.parseShort(split[0]));
                this.setSec(Short.parseShort(split[1]));
            }
            
        } else {
            this.setMin(Short.parseShort(duration));
        }
    }
    
    public void addDuration(Duration duration) {
        this.setTotalSeconds(this.getDurationInSeconds() + duration.getDurationInSeconds());
    }
    
    public void setHour(short hour) {
        this.hour = hour;
    }
    
    public void setMin(short min) {
        this.min = min;
    }
    
    public void setSec(short sec) {
        this.sec = sec;
    }
    
    public void setTotalSeconds(long seconds) {
        this.totalSeconds = seconds;
    }
    
    public void setDurationStartAsDate(Date start) {
        this.startDate = start;
    }
    
    public Date getStartDate() {
        return this.startDate;
    }
    
    public void setDurationEndAsDate(Date end) {
        SimpleDateFormat dayFormat = new SimpleDateFormat("D");
        SimpleDateFormat hourFormat = new SimpleDateFormat("HH");
        SimpleDateFormat minFormat = new SimpleDateFormat("mm");
        SimpleDateFormat secFormat = new SimpleDateFormat("ss");
        
        int startedDay = Integer.parseInt(dayFormat.format(this.startDate));
        int startedHour = Integer.parseInt(hourFormat.format(this.startDate));
        int startedMin = Integer.parseInt(minFormat.format(this.startDate));
        int startedSec = Integer.parseInt(secFormat.format(this.startDate));
        
        int endedDay = Integer.parseInt(dayFormat.format(end));
        int endedHour = Integer.parseInt(hourFormat.format(end));
        int endedMin = Integer.parseInt(minFormat.format(end));
        int endedSec = Integer.parseInt(secFormat.format(end));
        
        long startedTotal = (startedDay * 24 * 3600) + (startedHour * 3600) + (startedMin * 60) + startedSec;
        long endedTotal = (endedDay * 24 * 3600) + (endedHour * 3600) + (endedMin * 60) + endedSec;
        
        this.setTotalSeconds(endedTotal - startedTotal);
    }
    
    public String getHour() {
        String rval = null;
        if (totalSeconds == -1) {
            rval = ((this.hour < 10) ? "0" + this.hour : this.hour + "");
        } else {
            if (totalSeconds < 3600) {
                rval = "00";
            } else {
                long offset = totalSeconds % 3600;
                long hours = (totalSeconds - offset) / 3600;
                rval = (hours < 10 ? "0" + hours : hours + "");
            }
        }
        return rval;
    }
    
    public String getMin() {
        String rval = null;
        if (totalSeconds == -1) {
            rval = ((this.min < 10) ? "0" + this.min : this.min + "");
        } else {
            if (totalSeconds < 60) {
                rval = "00";
            } else {
                long offset = totalSeconds % 3600;
                long minutes = 0;
                if (offset == totalSeconds) {
                    offset = totalSeconds % 60;
                    minutes = (totalSeconds - offset) / 60;
                } else {
                    long tmpOffset = offset % 60;
                    minutes = (offset - tmpOffset) / 60;
                }
                rval = (minutes < 10 ? "0" + minutes : minutes + "");
            }
        }
        return rval;
    }
    
    public String getSec() {
        String rval = null;
        if (totalSeconds == -1) {
            rval = ((this.sec < 10) ? "0" + this.sec : this.sec + "");
        } else {
            if (totalSeconds < 60) {
                rval = (totalSeconds < 10 ? "0" + totalSeconds : totalSeconds + "");
            } else {
                long remainder = totalSeconds % 60;
                rval = (remainder < 10 ? "0" + remainder : remainder + "");
            }
        }
        return rval;
    }
    
    public String getDuration() {
        return (!this.getHour().equalsIgnoreCase("00") ? this.getHour() + ":" : "") + this.getMin() + ":" + this.getSec();
    }
    
    public long getDurationInSeconds() {
        long rval = -1;
        if (this.totalSeconds != -1) {
            rval = this.totalSeconds;
        } else {
            rval = (this.hour * 3600) + (this.min * 60) + this.sec;
        }
        return rval;
    }
    
    public String toString() {
        return "Current Zone - [" + this.getDuration() + "]";
    }
}
